package com.woniu.account.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.account.dao.TLoginLogMapper;
import com.woniu.account.dto.LoadAllLoginFormDTO;
import com.woniu.account.dto.LoginLogDto;
import com.woniu.account.dto.PageLoadAllLoginLogDTO;
import com.woniu.account.form.LoadAllLoginLogForm;
import com.woniu.account.model.TLoginLog;
import com.woniu.account.service.TLoginLogService;
import com.woniu.core.util.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 吴彦祖
 * @since 2024年04月02日
 */
@Service
public class TLoginLogServiceImpl extends ServiceImpl<TLoginLogMapper, TLoginLog> implements TLoginLogService {

    @Resource
    private TLoginLogMapper tLoginLogMapper;
    @Override
    public  PageInfo<LoginLogDto> loadAllLoginLog(LoadAllLoginLogForm form) throws Exception {
        Page<TLoginLog> page = new Page<>(form.getPageNum()==null?1:form.getPageNum(),form.getPageSize()==null?10:form.getPageSize());
        QueryWrapper<TLoginLog> qw = new QueryWrapper<>();
        if(ObjectUtil.isAllNotEmpty(form.getLogDate())){
            qw.eq("log_date",form.getLogDate());
        }
        if(ObjectUtil.isAllNotEmpty(form.getLoginRole())){
            qw.eq("login_role",form.getLoginRole());
        }
        if(ObjectUtil.isAllNotEmpty(form.getAccountName())){
            qw.like("account_name",form.getAccountName());
        }
        tLoginLogMapper.selectPage(page, qw);
        List<TLoginLog> list = page.getRecords();
        ArrayList<LoginLogDto> data = new ArrayList<>();
        for (TLoginLog loginLog : list) {
            LoginLogDto dto = BeanUtil.toBean(loginLog, LoginLogDto.class);
            data.add(dto);
        }
        PageInfo<LoginLogDto> pageInfo = new PageInfo<>();
        pageInfo.setTotal(Long.valueOf(data.size()));
        pageInfo.setList(data);
        pageInfo.setPages(page.getPages());
        pageInfo.setCurrent(page.getCurrent());
        pageInfo.setSize(page.getSize());
        return pageInfo;
    }
}
